﻿'Description:新登录和菜单相关
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:Wangjh 
'create Date:20211229
'Rewriter:
'Rewrite Date:
Imports DatabaseAccess.DBUtility
Imports BusinessRule.DTO
Public Class uLMSHomeBusiness
    ''' <summary>
    ''' 获取用户关联菜单
    ''' </summary>
    ''' <param name="userName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetMenuInfoByUser(ByVal userName As String) As Dictionary(Of String, List(Of MenuInfoDTO))

        Dim result = New Dictionary(Of String, List(Of MenuInfoDTO))

        Dim subMenuInfo = New List(Of MenuInfoDTO) '左侧显示的,一级菜单下的所有子菜单

        Dim subPageInfo = New List(Of MenuInfoDTO) '工作区显示，一级菜单下的页面

        Dim strSql = <string> SELECT MI.CAPTION, MI.WORKINGSECTIONURL, MI.URL, MI.SEQUENCE, MI.MENUITEMTYPE, SM.MENUDEFINITIONNAME SUBMENUNAME
                  ,mi.menudefinitionid FROM MENUDEFINITION M
                  LEFT JOIN MENUITEM MI ON MI.PARENTID = M.MENUDEFINITIONID
                  LEFT JOIN MENUDEFINITION SM ON SM.MENUDEFINITIONID = MI.MENUDEFINITIONID
                  LEFT JOIN EMPLOYEE E  ON E.WEBMENUDEFINITIONID = M.MENUDEFINITIONID
                  where e.employeename='{0}' order by mi.sequence </string>.Value
        Dim dt = DatabaseHelper.Query_ReturnDatTable(String.Format(strSql, userName))
        If dt.Rows.Count = 0 Then
            Return result
        End If

        For Each dr As DataRow In dt.Rows
            Dim menuItem = New MenuInfoDTO
            If dr("MENUITEMTYPE").ToString = "1" Then
                menuItem.text = dr("CAPTION").ToString()
                menuItem.url = dr("URL").ToString()
                'menuItem.iconCls = "icon-more"
                subPageInfo.Add(menuItem)
            ElseIf dr("MENUITEMTYPE").ToString = "2" Then
                menuItem = GetChildMenu(dr("menudefinitionid").ToString())
                menuItem.text = dr("CAPTION").ToString()
                'menuItem.iconCls = "icon-more"
                menuItem.iconCls = "icon-subMenu"
                subMenuInfo.Add(menuItem)
            End If
        Next
        result.Add("subMenu", subMenuInfo)
        result.Add("subPage", subPageInfo)

        Return result

    End Function

    ''' <summary>
    ''' 获取子菜单
    ''' </summary>
    ''' <param name="id"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetChildMenu(ByVal id As String) As MenuInfoDTO

        Dim result = New MenuInfoDTO()
        result.iconCls = "icon-blank"
        Dim children = New List(Of MenuInfoDTO)
        Dim strSql = <string> select mi.caption,mi.url,mi.sequence,mi.MENUITEMTYPE,mi.menudefinitionid  from MENUDEFINITION m
                    LEFT JOIN MENUITEM MI ON MI.PARENTID = M.MENUDEFINITIONID
                    where m.menudefinitionid='{0}'
                    order by mi.sequence </string>.Value

        Dim dt = DatabaseHelper.Query_ReturnDatTable(String.Format(strSql, id))
        If dt.Rows.Count = 0 Then
            Return result
        End If
        For Each dr As DataRow In dt.Rows
            Dim menuItem = New MenuInfoDTO()
            If dr("MENUITEMTYPE").ToString = "1" Then
                menuItem.text = dr("CAPTION").ToString()
                menuItem.url = dr("URL").ToString()
                menuItem.iconCls = "icon-page" '"icon-blank"
            ElseIf dr("MENUITEMTYPE").ToString = "2" Then
                menuItem = GetChildMenu(dr("menudefinitionid").ToString())
                menuItem.text = dr("CAPTION").ToString()
                menuItem.iconCls = "icon-subMenu"
            End If
            children.Add(menuItem)
        Next

        result.children = children

        Return result

    End Function
End Class
